import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'listData.dart';

class FirstPage extends StatefulWidget {
  final Map arguments;

  const FirstPage({Key key, this.arguments}) : super(key: key);

  _FirstPageState createState() => _FirstPageState(arguments);
}

class _FirstPageState extends State<FirstPage> {
  final Map arguments;

  _FirstPageState(this.arguments);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            "嘉兴学院·易班",
            style: TextStyle(
              fontSize: 18,
            ),
          ),
          leading: IconButton(icon: Icon(Icons.person), onPressed: () {}),
          actions: <Widget>[
            IconButton(icon: Icon(Icons.search), onPressed: () {}),
            IconButton(icon: Icon(Icons.add), onPressed: () {}),
          ],
        ),
        body: SingleChildScrollView(
          child: LayoutDemo(),
        ));
  }
}

class LayoutDemo extends StatefulWidget {
  LayoutDemo({Key key}) : super(key: key);

  @override
  _LayoutDemoState createState() => new _LayoutDemoState();
}

class _LayoutDemoState extends State<LayoutDemo> {
  // List<Map> imgList = [
  //   {"url":"assets/images/login_bg.png"},
  //   {"url":"assets/images/login_bg.png"},
  //   {"url":"assets/images/login_bg.png"},
  // ];

  @override
  Widget build(BuildContext context) {
    int pageN = 0;
    List data = listData;
    if (data.length / 15 - data.length ~/ 15 > 0)
      pageN = data.length ~/ 15 + 1;
    else
      pageN = data.length ~/ 15;
    print("!!!!!!!!!!!!!!!!!!!!!!!!!!");
    print(pageN);

    return Column(
      children: <Widget>[
        Container(
          height: 160,
          width: double.infinity,
          child: AspectRatio(
            aspectRatio: 1 / 1,
            child: Swiper(
              
              itemBuilder: (BuildContext context, int index) {
                return Image.asset(
                  // imgList[index]["url"],
                  "assets/images/login_bg.png",
                  fit: BoxFit.fill,
                );
              },
              index: 0, //初始时下标位置
              loop: true, //循环轮播
              autoplay: true, //自动轮播
              // itemCount: imgList.length,
              itemCount: 3,
              pagination: SwiperPagination(
                //分页器
                builder: DotSwiperPaginationBuilder(
                  //自己设置分页器样式
                  color: Color.fromRGBO(207, 204, 204, 1),
                  size: 8,
                  activeSize: 8, //轮播到时分页器小圆点大小
                ),
              ),
              // control: SwiperControl(), //左右划动箭头
            ),
          ),
        ),
        Container(
            height: 285,
            child: Swiper(
              itemCount: pageN,
              itemBuilder: (BuildContext context, int index) {
                List<Widget> t = [];
                for (int i = index*15; i < (index+1)*15; i++) {
                  if(i>=data.length)
                    break;
                  t.add(MyLayout(data[i]["title"],data[i]["imageUrl"]));
                }
                return MyImageButton(data: t);
              },
              pagination: SwiperPagination(
                  builder: DotSwiperPaginationBuilder(
                //自己设置分页器样式
                color: Color.fromRGBO(207, 204, 204, 1),
                size: 8,
                activeSize: 8,
              )),
            )),
        Divider(
          //分割线
          height: 0.0, //上下内边距
          indent: 0.0, //缩进
          color: Color.fromRGBO(221, 221, 221, 1),
          thickness: 1,
        ),
        SizedBox(height: 15),
        SingleChildScrollView(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Column(
                children: <Widget>[
                  Text("易班签到",
                      style: TextStyle(
                        color: Colors.red,
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      )),
                  Text("活动开始啦",
                      style: TextStyle(
                        fontSize: 16,
                      )),
                ],
              ),
              SizedBox(width: 10),
              SizedBox(
                width: 1,
                height: 45,
                child: DecoratedBox(
                  decoration: BoxDecoration(color: Colors.grey),
                ),
              ),
              SizedBox(width: 10),
              Container(
                alignment: Alignment.centerLeft,
                child: Text(
                  "四六级英语签到答题，敢挑战吗？",
                  style: TextStyle(fontSize: 13),
                ),
              )
            ],
          ),
        ),
        SizedBox(height: 15),
        Divider(
          color: Color.fromRGBO(220, 221, 223, 1),
          thickness: 10, //分割线厚度
        ),
        More(),
        // Divider(
        //   color: Color.fromRGBO(219, 219, 219, 1),
        //   thickness: 1,),
      ],
    );
  }
}

class MyImageButton extends StatefulWidget {
  final List<Widget> data;

  MyImageButton({Key key, this.data}) : super(key: key);

  @override
  _MyImageButtonState createState() => new _MyImageButtonState(data);
}

class _MyImageButtonState extends State<MyImageButton> {
  final List<Widget> data;

  _MyImageButtonState(this.data);

  @override
  Widget build(BuildContext context) {
    int rowN = 0;
    List<Widget> rows = [];

    if (data.length / 5 - data.length ~/ 5 > 0)
      rowN = data.length ~/ 5 + 1;
    else
      rowN = data.length ~/ 5;
    for (int i = 1; i <= rowN; i++) {
      List<Widget> t = [];
      for (int j = (i - 1) * 5; j < i * 5; j++) {
        if (j >= data.length) break;
        t.add(data[j]);
      }
      rows.add(Row(
        // mainAxisAlignment: MainAxisAlignment.center, //让整行居中
        children: t,
      ));
    }

    return SingleChildScrollView(
      child: Column(
        children: rows,
      ),
    );
  }
}

class MyLayout extends StatelessWidget {
  final String title;
  final String imageUrl; //"assets/images/login_bg.png"

  MyLayout(this.title, this.imageUrl);

  //单个可点击组件(图片+文字)
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 10),
      child: InkWell(
        onTap: () {},
        child: Container(
          width: 70,
          height: 75,
          child: Column(
            children: <Widget>[
              ClipOval(
                  //设置圆形图片
                  child: Image.asset(
                imageUrl,
                width: 50,
                height: 50,
                fit: BoxFit.cover,
              )),
              Text(title,
                  style: TextStyle(color: Color.fromRGBO(147, 147, 147, 1))),
            ],
          ),
        ),
      ),
    );
  }
}

class More extends StatefulWidget {
  @override
  _MoreState createState() => _MoreState();
}

class _MoreState extends State<More> {
  bool _school = true;
  bool _info = false;
  bool _college = false;
  bool _class = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
          TextButton(
              onPressed: () {
                setState(() {
                  _school = true;
                  _info = false;
                  _college = false;
                  _class = false;
                });
              },
              child: Text("学校",
                  style: TextStyle(
                    color:
                        _school ? Colors.blue : Colors.black, //当处于不同页面时，按钮颜色不同
                  ))),
          TextButton(
              onPressed: () {
                setState(() {
                  _school = false;
                  _info = true;
                  _college = false;
                  _class = false;
                });
              },
              child: Text("易班资讯",
                  style: TextStyle(
                    color: _info ? Colors.blue : Colors.black,
                  ))),
          TextButton(
              onPressed: () {
                setState(() {
                  _school = false;
                  _info = false;
                  _college = true;
                  _class = false;
                });
              },
              child: Text("学院",
                  style: TextStyle(
                    color: _college ? Colors.blue : Colors.black,
                  ))),
          TextButton(
              onPressed: () {
                setState(() {
                  _school = false;
                  _info = false;
                  _college = false;
                  _class = true;
                });
              },
              child: Text(
                "班级",
                style: TextStyle(
                  color: _class ? Colors.blue : Colors.black,
                ),
              )),
        ]),
        Visibility(
          visible: _school,
          child: SingleChildScrollView(child: SchoolPage()),
        ),
        Visibility(visible: _info, child: Container(child: Text('2'))),
        Visibility(visible: _college, child: Container(child: Text('3'))),
        Visibility(visible: _class, child: Container(child: Text('4'))),
      ],
    );
  }
}

class SchoolPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: <Widget>[
        InkWell(
          onTap: () {},
          child: ListTile(
            title: Text("123123123"),
          ),
        ),
        Divider(
          color: Color.fromRGBO(240, 240, 240, 1),
          thickness: 1,
        ),
        InkWell(
          onTap: () {},
          child: ListTile(
            title: Text("123123123"),
          ),
        ),
        Divider(
          color: Color.fromRGBO(240, 240, 240, 1),
          thickness: 1,
        ),
        InkWell(
          onTap: () {},
          child: ListTile(
            title: Text("123123123"),
          ),
        ),
      ],
    );
  }
}
